package acm;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class WordCountInArticle {

	private static final String article = "Hello,my name is wu, nice to meet evertone. Thanks for you." +
			"I will give my best efort to you. Thank you very much";

	public static void main(String[] args) {
		// 通过空格，以及逗号和句号分割文章中的单词
		// 注意.在正则表达式中表示任何字符，需要转义
		// 对于文章还应该处理换行符等\n
		// 新建一个map用来存储 字符串以及出现的次数
		Map<String, Integer> map = new HashMap<String, Integer>();
		// String[] strs=article.split(" |,|\\.");
		// 还可以使用下面的办法去掉非字符
		String[] strs = article.split("\\W+");
		for (int i = 0; i < strs.length; i++) {
			// System.out.print(string+"  ");
			if (!map.containsKey(strs[i])) {
				map.put(strs[i], 1);
			} else {
				int count = map.get(strs[i]);
				map.put(strs[i], count + 1);
			}
		}
		Set<String> set = map.keySet();
		Iterator<String> iterator = set.iterator();
		while (iterator.hasNext()) {
			String temp = iterator.next();
			System.out.println(temp + ":" + map.get(temp) + "次");
		}

	}

}
